package com.ljfiction.book.activitys;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.ImageView;
import android.widget.ProgressBar;
import android.widget.RelativeLayout;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.google.gson.Gson;
import com.google.gson.JsonSyntaxException;
import com.ljfiction.book.R;
import com.ljfiction.book.Baner.RecommendBean;
import com.ljfiction.book.MyView.DividerItemDecoration;
import com.ljfiction.book.adapter.MorexiaobianitemAdapter;
import com.ljfiction.book.http.HttpManager;
import com.lzy.okhttputils.OkHttpUtils;
import com.lzy.okhttputils.callback.StringCallback;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnRefreshLoadMoreListener;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import okhttp3.Call;
import okhttp3.Response;

public class RecommendActivity extends BaseActivity {

    @BindView(R.id.back_recommend)
    ImageView backRecommend;
    @BindView(R.id.recyclerView_commend)
    RecyclerView recyclerViewCommend;
    @BindView(R.id.topbar_recommend)
    RelativeLayout topbarRecommend;
    @BindView(R.id.load_xiaobianmuch)
    ProgressBar loadXiaobianmuch;
    @BindView(R.id.refresh_xiaobiaomuch)
    SmartRefreshLayout refreshXiaobiaomuch;
    private MorexiaobianitemAdapter adapter;
    private List<RecommendBean.DataBean> listBook = new ArrayList<>();
    private Intent intent;
    private RecommendBean recommen;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_recommend);
        ButterKnife.bind(this);
        intent = new Intent(RecommendActivity.this, BookDetailsActivity.class);
        intView();
        intData();
    }

    private void intView() {
        adapter = new MorexiaobianitemAdapter(R.layout.morexiaobian_item, listBook);
        adapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                intent.putExtra("bookId", String.valueOf(listBook.get(position).getBookId()));
                startActivity(intent);
            }
        });
        recyclerViewCommend.setAdapter(adapter);
        recyclerViewCommend.setLayoutManager(new LinearLayoutManager(this));
        recyclerViewCommend.addItemDecoration(new DividerItemDecoration(this, DividerItemDecoration.VERTICAL_LIST));
        refreshXiaobiaomuch.setEnableLoadMore(false);
        refreshXiaobiaomuch.setOnRefreshLoadMoreListener(new OnRefreshLoadMoreListener() {
            @Override
            public void onLoadMore(RefreshLayout refreshlayout) {

            }

            @Override
            public void onRefresh(RefreshLayout refreshlayout) {
                listBook.clear();
                intData();
            }
        });
    }

    private void intData() {
        OkHttpUtils.post(HttpManager.IP + "/book-xiaobiantuijian")
                .execute(new StringCallback() {
                    @Override
                    public void onSuccess(String s, Call call, Response response) {
                        loadXiaobianmuch.setVisibility(View.GONE);
                        refreshXiaobiaomuch.finishRefresh();
                        Gson gs = new Gson();
                        try {
                            recommen = gs.fromJson(s, RecommendBean.class);
                            if (recommen.getReturnCode() == 200) {
                                listBook .addAll( recommen.getData());
                               adapter.notifyDataSetChanged();
                            }
                        } catch (Exception e) {
                            e.printStackTrace();
                        }
                    }
                });
    }

    @OnClick(R.id.back_recommend)
    public void onViewClicked() {
        goBack();
    }

    @Override
    protected void onResume() {
        topbarRecommend.getBackground().setAlpha(255);
        super.onResume();
    }
}
